/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月29日
 */
package com.massyframework.beanskin.runtime.beancontext;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.massyframework.beanskin.ModuleListener;
import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.assembly.AssemblyContext;
import com.massyframework.beanskin.assembly.AssemblyContextAware;
import com.massyframework.beanskin.assembly.AssemblyContextDelegate;
import com.massyframework.beanskin.assembly.AssemblyStatus;
import com.massyframework.beanskin.assembly.beans.BeanExistsException;
import com.massyframework.beanskin.assembly.beans.BeanRegistry;
import com.massyframework.beanskin.assembly.handling.ActivationHandler;
import com.massyframework.beanskin.assembly.handling.HandlerBase;
import com.massyframework.beanskin.assembly.handling.HandlerRegistration;
import com.massyframework.beanskin.assembly.handling.LifecycleEventAdapter;

/**
 * 简单Bean容器适配器
 * @author huangkh
 *
 */
final class SimpleBeanContextHandler extends HandlerBase 
	implements ActivationHandler, BeanRegistry {
	
	private SimpleBeanContext beanContext;
	private HandlerRegistration<?> registration;
	private EventListener listener;

	/**
	 * 
	 */
	public SimpleBeanContextHandler(SimpleBeanContext beanContext) {
		this.beanContext = Objects.requireNonNull(beanContext, "\"beanContext\" cannot be null.");
	}

	@Override
	protected synchronized void init() throws Exception {
		super.init();
		if (this.listener == null) {
			this.listener = new EventListener();
			this.getLifecycleManager().addListener(this.listener);
		}
	}

	@Override
	public synchronized void destroy() {
		if (this.listener != null) {
			this.getLifecycleManager().removeListener(this.listener);
			this.listener = null;
		}
		super.destroy();
	}

	@Override
	public synchronized void doStarting() throws Exception {
		if (this.registration == null) {
			AssemblyContextDelegate<SimpleBeanContext> assemblyContext = 
					new AssemblyContextDelegate<>(beanContext);		
			this.registration = this.getLifecycleManager().register(assemblyContext);
		}
	}

	@Override
	public synchronized void doStopped() throws Exception {
		if (this.registration != null) {
			this.registration.unregister();
			this.registration = null;
		}
	}
	
	@Override
	public synchronized void addBean(String beanName, Object bean) throws BeanExistsException {
		if (this.getLifecycleManager().getAssemblyStatus() != AssemblyStatus.WORKING) {
			this.beanContext.addBean(beanName, bean);
		}
	}

	@Override
	public synchronized boolean containsBean(String name) {
		return this.beanContext.containsBean(name);
	}

	@Override
	public synchronized Object getBean(String beanName) {
		return this.beanContext.getBean(beanName);
	}

	@Override
	public synchronized List<String> getBeanNames() {
		return this.beanContext.getBeanNames();
	}

	private class EventListener extends LifecycleEventAdapter {

		@Override
		public void onActivated() {
			super.onActivated();
		
			Assembly assembly = getAssociatedAssembly();
			AssemblyContext context = assembly.getAssemblyContext();
			Map<String, AssemblyContextAware> beanMap = context.getBeansOfType(AssemblyContextAware.class);
			for (AssemblyContextAware bean: beanMap.values()) {
				bean.setAssemblyContext(context);
			}
			
			Map<String, ModuleListener> listenerMap = context.getBeansOfType(ModuleListener.class);
			for (ModuleListener bean: listenerMap.values()) {
				assembly.addListener(bean);
			}
		}

		@Override
		public void onInactivating() {
			Assembly assembly = getAssociatedAssembly();
			AssemblyContext context = assembly.getAssemblyContext();
			
			Map<String, ModuleListener> listenerMap = context.getBeansOfType(ModuleListener.class);
			for (ModuleListener bean: listenerMap.values()) {
				assembly.removeListener(bean);
			}
			
			Map<String, AssemblyContextAware> beanMap = context.getBeansOfType(AssemblyContextAware.class);
			for (AssemblyContextAware bean: beanMap.values()) {
				bean.setAssemblyContext(null);
			}
			super.onInactivating();
		}
		
		
	}
	
}
